package com.shujia.flinkcore;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;

public class wordCountBat {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.BATCH);
        env.setParallelism(2);
        DataStreamSource<String> src = env.readTextFile("flink/data/words");
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordDS = src.flatMap((words, out) -> Arrays.asList(words.split(",")).forEach(e -> out.collect(e)), Types.STRING).map(word -> Tuple2.of(word, 1), Types.TUPLE(Types.STRING, Types.INT));
        SingleOutputStreamOperator<Tuple2<String, Integer>> sumDS = wordDS.keyBy(key -> key.f0).sum(1);
        sumDS.print();
        env.execute();
    }
}
